package com.ogilvy.audi.webapp.listener;


import com.ogilvy.audi.Constants;
import com.ogilvy.audi.model.Resource;
import com.ogilvy.audi.service.LookupManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: wutl
 * Date: 2010-8-7
 * Time: 17:52:06
 * To change this template use File | Settings | File Templates.
 */
public final class ApplicationUtil {
    private static final Log log = LogFactory.getLog(ApplicationUtil.class);
    public static final Map<String,String> SYS_PARS=new HashMap();
    //public static final Map<Resource,Collection<ConfigAttribute>> AUTHOR_RESOURCE_MAP=new HashMap();

    public static void refreshApplication(ServletContext context) {
        log.debug("refresh application attribute");
        catchUrlAuthorities(context);
     //   catchAllSystemParameters(context);
    }

       /**
     * 缓存URL权限
     * @param context
     */
    private static void catchUrlAuthorities(ServletContext context) {
        ApplicationContext ctx =
                WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        LookupManager mgr = (LookupManager) ctx.getBean("lookupManager");
        List<Resource> frontUrlAuthorities = mgr.loadUrlAuthorities("frontUrl");
        List<Resource> backUrlAuthorities = mgr.loadUrlAuthorities("backUrl");
        List<Resource> defaultUrlAuthorities = mgr.loadUrlAuthorities("defaultUrl");
         //  Map<Resource,Collection<ConfigAttribute>> authorMap=mgr.loadAllUrlAuthorities();
        context.setAttribute(Constants.DEFAULT_FRONT_URL_AUTHORITIES_NAME, frontUrlAuthorities);
        context.setAttribute(Constants.DEFAULT_BACK_URL_AUTHORITIES_NAME, backUrlAuthorities);
        context.setAttribute(Constants.DEFAULT_URL_AUTHORITIES_NAME, defaultUrlAuthorities);
    //         synchronizedMap(AUTHOR_RESOURCE_MAP,authorMap);

    }






//    private static void catchAllSystemParameters(ServletContext context){
//            ApplicationContext ctx =
//                WebApplicationContextUtils.getRequiredWebApplicationContext(context);
//            LookupManager mgr = (LookupManager) ctx.getBean("lookupManager");
//             Map pars=mgr.loadSystemParameters();
//        synchronizedMap(SYS_PARS,pars);
//
//    }



  private static  void  synchronizedMap(Map map,Map dates){
        synchronized(map) {
            Set<String> keySet = map.keySet();
            String[] keys = new String[keySet.size()];
            for(String key : keySet.toArray(keys)) {
                if(!dates.containsKey(key)) {
                   map.remove(key);
                }
            }

            map.putAll(dates);
        }
    }




    
}